package cn.laixueit.service.impl;

import cn.laixueit.mapper.UsersMapper;
import cn.laixueit.pojo.Users;
import cn.laixueit.pojo.UsersExample;
import cn.laixueit.service.UserService;
import cn.laixueit.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.List;

@Service
@Transactional(rollbackFor = RuntimeException.class)
public class UserServiceImpl implements UserService {

    @Autowired
    private UsersMapper usersMapper;

    @Override
    public void saveUser(Users users) {
        users.setId(Utils.getUUID(6));
        usersMapper.insertSelective(users);
    }

    @Override
    public int login(Users users) {
        String password = users.getPassword();
        String md5password = DigestUtils.md5DigestAsHex(password.getBytes());
        users.setPassword(md5password);
        UsersExample example = new UsersExample();
        UsersExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(users.getUsername()).andPasswordEqualTo(users.getPassword());
        List<Users> usersList = usersMapper.selectByExample(example);
        return usersList.size();
    }
}
